package com.starchen.optimalselection.admin.controller;

import com.starchen.optimalselection.admin.model.AdminUser;
import com.starchen.optimalselection.admin.model.form.AssginRoleForm;
import com.starchen.optimalselection.admin.model.vo.UserRoleVo;
import com.starchen.optimalselection.admin.service.AdminUserRoleService;
import com.starchen.optimalselection.admin.service.AdminUserService;
import com.starchen.optimalselection.common.model.QueryParamsForm;
import com.starchen.optimalselection.common.model.ResultPage;
import com.starchen.optimalselection.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

/**
 * @author daxilong
 * @date 2024/12/5 23:09
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    private AdminUserService sysUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AdminUserRoleService adminUserRoleService;

    @ApiOperation("用户列表")
    @GetMapping("/page")
    public Result list(QueryParamsForm queryParamsVo) {
        ResultPage pageModel = sysUserService.selectPage(queryParamsVo);
        return Result.success(pageModel);
    }

    @ApiOperation("添加用户")
    @PostMapping("/create")
    public Result save(@RequestBody AdminUser user) {
        String encodePassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encodePassword);
        boolean is_Success = sysUserService.save(user);

        if(is_Success) {
            return Result.success();
        } else {
            return Result.error();
        }
    }

    @ApiOperation("根据id查询")
    @GetMapping("/info/{id}")
    public Result getUser(@PathVariable Long id) {
        AdminUser user = sysUserService.getById(id);
        return Result.success(user);
    }

    @ApiOperation("修改用户")
    @PostMapping("/update")
    public Result update(@RequestBody AdminUser user) {
        if(user.getPassword() != null){
            String encodePassword = passwordEncoder.encode(user.getPassword());
            user.setPassword(encodePassword);
        }
        boolean is_Success = sysUserService.updateById(user);
        if(is_Success) {
            return Result.success();
        } else {
            return Result.error();
        }
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/delete/{id}")
    public Result remove(@PathVariable Long id) {
        boolean is_Success = sysUserService.removeById(id);
        if(is_Success) {
            return Result.success();
        } else {
            return Result.error();
        }
    }

    @ApiOperation("获取用户的角色数据")
    @GetMapping("getrole/{id}")
    public Result getRole(@PathVariable Long id) {
        UserRoleVo userRole = adminUserRoleService.getUserRole(id);
        return Result.success(userRole);
    }

    @ApiOperation("给用户分配角色")
    @PostMapping("setrole")
    public Result doAssign(@RequestBody AssginRoleForm assginRoleForm) {
        adminUserRoleService.relationSet(assginRoleForm);
        return Result.success();
    }

}
